//
//  AXFTabBarController.m
//  AXFEightGroup
//
//  Created by ooops on 12/22/16.
//  Copyright © 2016 ya. All rights reserved.
//

#import "AXFTabBarController.h"
#import "AXFNavigationController.h"
#import "AXFTransitioningAnimator.h"
#import "CSTransitioningAnimator.h"

@interface AXFTabBarController () <UITabBarControllerDelegate>

@property (nonatomic, strong) UIViewController *tmpVC;
@property (nonatomic, strong) CSTransitioningAnimator *animator;

@end

@implementation AXFTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setChildViewControllers];

    self.delegate = self;

    
    
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if (self.tmpVC == viewController) {
        UIViewController *vc = [self createChildController:@"AXFShopViewController"    title:@"购物车"     imageName:@"v2_shopCart"];
        self.animator = [CSTransitioningAnimator transitioningAnimatorWithPresentViewController:vc];
        vc.view.backgroundColor = [UIColor whiteColor];
        [self presentViewController:vc animated:YES completion:nil];
        return NO;
    }

    return YES;
}

/**
 搭建tabbar界面
 */
- (void)setChildViewControllers {

    NSMutableArray *vcArray = [NSMutableArray new];
    vcArray[0] = [self createChildController:@"AXFHomeViewController"    title:@"主页"      imageName:@"v2_home"];
    vcArray[1] = [self createChildController:@"AXFOrderViewController"   title:@"闪电超市"   imageName:@"v2_order"];
    vcArray[2] = [self createChildController:@"AXFShopViewController"    title:@"购物车"     imageName:@"v2_shopCart"];
    vcArray[3] = [self createChildController:@"AXFMyViewController"      title:@"我"         imageName:@"v2_my"];
    self.viewControllers = vcArray.copy;

    self.tmpVC = vcArray[2];
}


- (UIViewController *)createChildController:(NSString *)controller title:(NSString *)title imageName:(NSString *)imageName {
    //字符串转类
    Class cls = NSClassFromString(controller);
    UIViewController *baseVC = [[cls alloc] init];
    baseVC.title = title;

    //取消系统图片渲染模式
    baseVC.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    baseVC.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_r",imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //文字和图标间距
    baseVC.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -3);
    //设置文字的颜色
    [baseVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
    //导航控制器包装
    AXFNavigationController *nav = [[AXFNavigationController alloc] initWithRootViewController:baseVC];
    return nav;
}

@end
